package com.huipinche.view.impl;

import com.huipinche.dao.bean.Journey;
import com.huipinche.view.FindPageView;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Date;

import com.huipinche.view.utils.*;
import com.huipinche.service.impl.*;

public class FindPageViewimpl  implements FindPageView {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField txtAaa;
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;
    private final JLabel lblNewLabel = new JLabel("\u53D1\u8D77\u62FC\u5355");
    private JButton btnNewButton_1;
    private int page = 0;
    private boolean flag = true;
    private JFrame jFrame;
    final int WIDTH = 497;
    final int HEIGHT = 551;

    /**
     * Launch the application.
     */
  /*  public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FindPageViewimpl frame = new FindPageViewimpl();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }*/

    /**
     * Create the frame.
     */
    public void initialize(){
        jFrame = new JFrame("惠拼车—查找页面");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2,
                (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        try {
            jFrame.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("logo3.jpeg"))));
        } catch (IOException e) {
            e.printStackTrace();
        }

        jFrame.setTitle("\u60E0\u62FC\u8F66-\u67E5\u627E\u8BA2\u5355");
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // setBounds(100, 100, 497, 551);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        jFrame.setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setEditable(false);
        textField.setSelectedTextColor(new Color(192, 192, 192));
        textField.setSelectionColor(new Color(192, 192, 192));
        textField.setForeground(new Color(176, 196, 222));
        textField.setBorder(null);
        textField.setOpaque(false);
        textField.setAlignmentX(Component.LEFT_ALIGNMENT);
        textField.setFont(new Font("微软雅黑", Font.BOLD | Font.ITALIC, 15));
        textField.setBounds(90, 45, 66, 34);
        textField.setText("出发地址");
        contentPane.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(189, 48, 133, 28);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setEditable(false);
        textField_2.setSelectionColor(new Color(192, 192, 192));
        textField_2.setSelectedTextColor(new Color(192, 192, 192));
        textField_2.setForeground(new Color(176, 196, 222));
        textField_2.setOpaque(false);
        textField_2.setBorder(null);
        textField_2.setFont(new Font("微软雅黑", Font.BOLD | Font.ITALIC, 15));
        textField_2.setBounds(90, 112, 66, 34);
        textField_2.setText("起始日期");
        contentPane.add(textField_2);
        textField_2.setColumns(10);

        txtAaa = new JTextField();
        txtAaa.setOpaque(false);
        txtAaa.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
        txtAaa.setForeground(Color.CYAN);

        txtAaa.setBounds(31, 243, 300, 173);
        contentPane.add(txtAaa);
        txtAaa.setColumns(20);

        textField_4 = new JTextField();
        textField_4.setBounds(189, 112, 133, 28);
        contentPane.add(textField_4);
        textField_4.setColumns(10);

        textField_5 = new JTextField();
        textField_5.setEditable(false);
        textField_5.setForeground(new Color(176, 196, 222));
        textField_5.setOpaque(false);
        textField_5.setBorder(null);
        textField_5.setFont(new Font("微软雅黑", Font.BOLD | Font.ITALIC, 15));
        textField_5.setBounds(90, 181, 66, 37);
        textField_5.setText("截止日期");
        contentPane.add(textField_5);
        textField_5.setColumns(10);

        textField_6 = new JTextField();
        textField_6.setBounds(189, 185, 133, 28);
        contentPane.add(textField_6);
        textField_6.setColumns(10);

        JButton btnNewButton00 = new JButton("返回主页");
        FindPageViewimpl temp = this;
        btnNewButton00.setBounds(0, 0, 89, 23);
        btnNewButton00.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                temp.page = 200;//homepage
                temp.flag = false;
            }
        });
        contentPane.setLayout(null);
        contentPane.add(btnNewButton00);

        JButton btnNewButton = new JButton("查找");
        btnNewButton.setBorder(BorderFactory.createRaisedBevelBorder());
        Icon select = new ImageIcon((PathUtils.getRealPath("car3.png")));
        btnNewButton.setIcon(select);
        btnNewButton.setFont(new Font("华文楷体", Font.BOLD, 16));
        btnNewButton.setBounds(349, 108, 122, 36);
        btnNewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            //    System.out.println("最后一步了"+textField_1.getText());
                Journey[] journeys = new findJourneyServiceimpl().findJourneyByadd(
                        (textField_1.getText()));
                System.out.println(journeys[0].toString());
            }
        });
        contentPane.add(btnNewButton);
        btnNewButton_1 = new JButton("拼单");
        //传到拼单大厅
        btnNewButton_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                temp.page = 232;//拼单大厅
                temp.flag = false;
            }
        });
        btnNewButton_1.setIcon(select);
        // btnNewButton_1.setForeground(new Color(176, 196, 222));
        btnNewButton_1.setBounds(349, 300, 122, 36);
        btnNewButton_1.setFont(new Font("华文楷体", Font.BOLD, 16));
        contentPane.add(btnNewButton_1);
        lblNewLabel.setIcon(new ImageIcon(PathUtils.getRealPath("bigbackground4.png")));
        lblNewLabel.setBounds(0, 0, 481, 512);
        contentPane.add(lblNewLabel);

        jFrame.setVisible(true);
        jFrame.setResizable(false);
    }

    @Override
    public int FindPage(boolean flag) throws InterruptedException {
            this.flag = flag;
            initialize();
            while(this.flag){
            Thread.sleep(200);
           };
            switch (page){
                case 200:
                   jFrame.removeNotify();
                    return 200;//返回主页
                case 232:
                   jFrame.removeNotify();
                    return 232;//拼单成功，提示页面
            }
        return 0;
    }
}
